﻿using Sidvall.WorkItemManager.Workflow;
using System.Linq;

namespace Sidvall.WorkItemManager.UI.Entities
{
    public sealed partial class WorkItem
    {
        #region Public Members

        #region LastExecutionTime

        public System.TimeSpan? LastExecutionTime
        {
            get
            {
                if ((this.LastExecutionStartTime != null) && (this.LastExecutionEndTime != null))
                    return this.LastExecutionEndTime.Value.ToUniversalTime().Subtract(this.LastExecutionStartTime.Value.ToUniversalTime());
                return null;
            }
        }

        #endregion
        #region LatestActivity

        public System.DateTime? LatestActivity
        {
            get
            {
                return Sidvall.WorkItemManager.SystemContext.Current.EntityFacade.WorkItemManager.GetLatestActivity(this);
            }
        }

        #endregion
        #region ResultTypeName

        public string ResultTypeName
        {
            get
            {
                return this.LastWorkItemResultId.ToString();
            }
        }

        #endregion
        #region WorkItemRating

        private WorkItemRating _WorkItemRating;
        public WorkItemRating WorkItemRating
        {
            get
            {
                return _WorkItemRating;
            }
            set
            {
                _WorkItemRating = value;
                OnPropertyChanged("WorkItemRating", true, false);
            }
        }

        #endregion

        #region ImportResult

        public void ImportResult(Sidvall.WorkItemManager.Data.IWorkItem item)
        {
            Sidvall.Data.CopySettings copySettings;

            if (item == null)
                return;
            using (var manager = new Sidvall.Business.BusinessSettingsManager(this, Sidvall.Business.BusinessEvents.NotifyPropertyChanged))
            {
                this.ExecutionCompletionPercentage = item.ExecutionCompletionPercentage;
                this.ExecutionContext = item.ExecutionContext;
                this.ExecutionContextUpdated = item.ExecutionContextUpdated;
                this.ExecutionStartTime = item.ExecutionStartTime;
                this.FailedExecutionCount = item.FailedExecutionCount;
                this.LastExecutionEndTime = item.LastExecutionEndTime;
                this.LastExecutionStartTime = item.LastExecutionStartTime;
                this.LastResultMessage = item.LastResultMessage;
                this.LastWorkItemResultId = item.LastWorkItemResultId;
                this.WorkItemStatusId = item.WorkItemStatusId;
                this.RetryCount = item.RetryCount;
                this.RetryTime = item.RetryTime;
                this.ForceExecution = item.ForceExecution;
                this.IsCanceled = item.IsCanceled;
                if ((item.WorkItemSensors != null) && (item.WorkItemSensors.Any()))
                {
                    copySettings = new Sidvall.Data.CopySettings(Sidvall.Business.FieldCopyOptions.All, Sidvall.Data.DataRowContextOption.None, Sidvall.Data.DataRowState.Unchanged);
                    this.LoadWorkItemSensors(Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.Copy(item.WorkItemSensors, copySettings));
                }
                else
                {
                    this.LoadWorkItemSensors(new WorkItemSensorCollection());
                }
            }
        }

        #endregion
        #region OnAfterPropertyChanged

        partial void OnAfterExecutionContextUpdatedPropertyChanged()
        {
            OnPropertyChanged(nameof(LatestActivity), true, false);
        }
        partial void OnAfterExecutionStartTimePropertyChanged()
        {
            OnPropertyChanged(nameof(LatestActivity), true, false);
        }
        partial void OnAfterLastWorkItemResultIdPropertyChanged()
        {
            OnPropertyChanged(nameof(ResultTypeName), true, false);
        }
        partial void OnAfterLastExecutionEndTimePropertyChanged()
        {
            OnPropertyChanged(nameof(LastExecutionTime), true, false);
            OnPropertyChanged(nameof(LatestActivity), true, false);
        }
        partial void OnAfterLastExecutionStartTimePropertyChanged()
        {
            OnPropertyChanged(nameof(LastExecutionTime), true, false);
            OnPropertyChanged(nameof(LatestActivity), true, false);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return this.Name;
        }

        #endregion

        #endregion
    }
}